﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using EZCRS.HCMS.Entity;
using HCMS.Utility;

namespace EZCRS.HCMS.Bll.AIR
{
    /// <summary>
    /// 房价数据转换类
    /// Jay.Gu
    /// 2009-04-09
    /// 用法：Load方法讲数据对象装入，修改某日的数据使用ADD方法
    /// </summary>
    public class HotelRateProvider
    {
        private DataSet _ds1_R = new DataSet();
        private DataSet _ds2_R = new DataSet();
        private DataSet _ds3_R = new DataSet();
        private DataSet _ds4_R = new DataSet();
        private DataSet _ds5_R = new DataSet();
        private DataSet _ds6_R = new DataSet();
        private DataSet _ds7_R = new DataSet();
        private DataSet _ds8_R = new DataSet();
        private DataSet _ds9_R = new DataSet();
        private DataSet _ds10_R = new DataSet();
        private DataSet _ds11_R = new DataSet();
        private DataSet _ds12_R = new DataSet();

        private DataSetUtility _utility = new DataSetUtility();

        private int _iYear;

        /// <summary>
        /// 年
        /// </summary>
        public int Year
        {
            get
            {
                return _iYear;
            }
            set
            {
                _iYear = value;
            }
        }

        #region 每月数据

        public DataSet January
        {
            get
            {
                return _ds1_R;
            }
        }

        public DataSet February
        {
            get
            {
                return _ds2_R;
            }
        }
        public DataSet March
        {
            get
            {
                return _ds3_R;
            }
        }
        public DataSet April
        {
            get
            {
                return _ds4_R;
            }
        }
        public DataSet May
        {
            get
            {
                return _ds5_R;
            }
        }
        public DataSet June
        {
            get
            {
                return _ds6_R;
            }
        }

        public DataSet July
        {
            get
            {
                return _ds7_R;
            }
        }

        public DataSet August
        {
            get
            {
                return _ds8_R;
            }
        }

        public DataSet September
        {
            get
            {
                return _ds9_R;
            }
        }

        public DataSet October
        {
            get
            {
                return _ds10_R;
            }
        }

        public DataSet November
        {
            get
            {
                return _ds11_R;
            }
        }

        public DataSet December
        {
            get
            {
                return _ds12_R;
            }
        }

        #endregion

        /// <summary>
        /// 按日期范围加载, 日期必须同一年内
        /// </summary>
        /// <param name="StartDate">开始日期</param>
        /// <param name="Week">每日有效性</param>
        /// <param name="EndDate">结束日期</param>
        /// <param name="Rate1">单人价格</param>
        /// <param name="Rate2">双人价格</param>
        /// <param name="Rate3">3人价</param>
        /// <param name="Rate4">4人价</param>
        /// <param name="Bed">加床价</param>
        /// <param name="Breakfast">是否含早</param>
        /// <param name="BreakfastNum">早餐数量</param>
        /// <param name="BreakfastPrice">早餐价格</param>
        public void Create(DateTime StartDate, DateTime EndDate, int[] Week, float Rate1, float Rate2, float Rate3, float Rate4, float Bed,
                            bool Breakfast, int BreakfastNum, float BreakfastPrice)
        {

            if (EndDate < StartDate)
                return;

            //不允许跨年
            if (StartDate.Year != EndDate.Year)
                return;

            _iYear = StartDate.Year;

            //int iMonth = EndDate.Year * 12 + EndDate.Month - StartDate.Year * 12 - StartDate.Month;

            TimeSpan span = EndDate - StartDate;

            for (int i = 0; i <= span.Days; i++)
            {
                DateTime day = StartDate.AddDays(i);

                if (!SkipDate(day, Week))
                {
                    #region 写入DataSet

                    switch (day.Month)
                    {
                        case 1:

                            if (_ds1_R == null)
                                _ds1_R = new DataSet();

                            Add(_ds1_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;

                        case 2:

                            if (_ds2_R == null)
                                _ds2_R = new DataSet();
                            Add(_ds2_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;

                        case 3:

                            if (_ds3_R == null)
                                _ds3_R = new DataSet();
                            Add(_ds3_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;

                        case 4:

                            if (_ds4_R == null)
                                _ds4_R = new DataSet();
                            Add(_ds4_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;
                        case 5:

                            if (_ds5_R == null)
                                _ds5_R = new DataSet();
                            Add(_ds5_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;
                        case 6:

                            if (_ds6_R == null)
                                _ds6_R = new DataSet();
                            Add(_ds6_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;
                        case 7:

                            if (_ds7_R == null)
                                _ds7_R = new DataSet();
                            Add(_ds7_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;

                        case 8:

                            if (_ds8_R == null)
                                _ds8_R = new DataSet();
                            Add(_ds8_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;

                        case 9:

                            if (_ds9_R == null)
                                _ds9_R = new DataSet();
                            Add(_ds9_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;

                        case 10:

                            if (_ds10_R == null)
                                _ds10_R = new DataSet();

                            Add(_ds10_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;

                        case 11:

                            if (_ds11_R == null)
                                _ds11_R = new DataSet();

                            Add(_ds11_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;

                        case 12:

                            if (_ds12_R == null)
                                _ds12_R = new DataSet();

                            Add(_ds12_R, day, Rate1, Rate2, Rate3, Rate4, Bed, Breakfast, BreakfastNum, BreakfastPrice);

                            break;
                    }

                    #endregion
                }

            }

        }

        /// <summary>
        /// 检查日期是否需要跳过
        /// </summary>
        /// <param name="Day"></param>
        /// <param name="Week"></param>
        /// <returns></returns>
        private bool SkipDate(DateTime Day, int[] Week)
        {
            try
            {
                if (Day.DayOfWeek == DayOfWeek.Monday)
                {
                    if (Week[1] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Tuesday)
                {
                    if (Week[2] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Wednesday)
                {
                    if (Week[3] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Thursday)
                {
                    if (Week[4] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Friday)
                {
                    if (Week[5] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Saturday)
                {
                    if (Week[6] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Sunday)
                {
                    if (Week[0] == 0)
                        return true;
                }
            }
            catch { }

            return false;

        }


        /// <summary>
        /// 修改每月R字段值
        /// </summary>
        private void Add(DataSet ds, DateTime StartDate, float Rate1, float Rate2, float Rate3, float Rate4, float Bed, bool BF, int BFnum, float BFrate)
        {
            CreateDataSet(ds);

            DataRow[] rows = ds.Tables[0].Select("Date = '" + StartDate.ToString("yyyy-MM-dd") + "'");

            if (rows.Length == 0)
            {
                DataRow dr = ds.Tables[0].NewRow();

                dr["Date"] = StartDate;

                dr["Rate1"] = Rate1;
                dr["Rate2"] = Rate2;

                dr["Rate3"] = Rate3;
                dr["Rate4"] = Rate4;

                dr["Bed"] = Bed;

                dr["BF"] = BF;
                dr["BFnum"] = BFnum;
                dr["BFrate"] = BFrate;

                ds.Tables[0].Rows.Add(dr);
            }
            else
            {

                rows[0]["Rate1"] = Rate1;
                rows[0]["Rate2"] = Rate2;

                rows[0]["Rate3"] = Rate3;
                rows[0]["Rate4"] = Rate4;

                rows[0]["Bed"] = Bed;

                rows[0]["BF"] = BF;
                rows[0]["BFnum"] = BFnum;
                rows[0]["BFrate"] = BFrate;
            }
        }

        /// <summary>
        /// 初始化DataSet
        /// </summary>
        /// <param name="ds"></param>
        protected void CreateDataSet(DataSet ds)
        {
            if (ds.Tables.Count == 0)
            {
                DataTable dt = new DataTable();

                dt.Columns.Add("Date", typeof(DateTime)); //日期

                dt.Columns.Add("Rate1", typeof(float)); //单人价
                dt.Columns.Add("Rate2", typeof(float)); //双人价
                dt.Columns.Add("Rate3", typeof(float)); //三人价
                dt.Columns.Add("Rate4", typeof(float)); //四人价

                dt.Columns.Add("Bed", typeof(float));    //加床价

                dt.Columns.Add("BF", typeof(bool));      //是否含早
                dt.Columns.Add("BFnum", typeof(float));  //早餐数量
                dt.Columns.Add("BFrate", typeof(float)); //早餐价格

                ds.Tables.Add(dt);
            }

        }


        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="PropertyID"></param>
        /// <param name="Year"></param>
        /// <param name="PlanCode"></param>
        /// <param name="RoomCode"></param>
        public void Load(HCRS_AIR AIR)
        {
            _iYear = AIR.Year.Value;

            if (AIR.January_R != null)
                _ds1_R = _utility.RetrieveDataSet(AIR.January_R);

            if (AIR.February_R != null)
                _ds2_R = _utility.RetrieveDataSet(AIR.February_R);

            if (AIR.March_R != null)
                _ds3_R = _utility.RetrieveDataSet(AIR.March_R);

            if (AIR.April_R != null)
                _ds4_R = _utility.RetrieveDataSet(AIR.April_R);

            if (AIR.May_R != null)
                _ds5_R = _utility.RetrieveDataSet(AIR.May_R);

            if (AIR.June_R != null)
                _ds6_R = _utility.RetrieveDataSet(AIR.June_R);

            if (AIR.July_R != null)
                _ds7_R = _utility.RetrieveDataSet(AIR.July_R);

            if (AIR.August_R != null)
                _ds8_R = _utility.RetrieveDataSet(AIR.August_R);

            if (AIR.September_R != null)
                _ds9_R = _utility.RetrieveDataSet(AIR.September_R);

            if (AIR.October_R != null)
                _ds10_R = _utility.RetrieveDataSet(AIR.October_R);

            if (AIR.November_R != null)
                _ds11_R = _utility.RetrieveDataSet(AIR.November_R);

            if (AIR.December_R != null)
                _ds12_R = _utility.RetrieveDataSet(AIR.December_R);

        }

        /// <summary>
        /// 输出byte数组
        /// </summary>
        /// <param name="Month">月份</param>
        /// <returns></returns>
        public byte[] ConvertToBinaryData(int Month)
        {
            switch (Month)
            {
                case 1:
                    if (_ds1_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds1_R);
                case 2:
                    if (_ds2_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds2_R);
                case 3:
                    if (_ds3_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds3_R);
                case 4:
                    if (_ds4_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds4_R);
                case 5:
                    if (_ds5_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds5_R);
                case 6:
                    if (_ds6_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds6_R);
                case 7:
                    if (_ds7_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds7_R);
                case 8:
                    if (_ds8_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds8_R);
                case 9:
                    if (_ds9_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds9_R);
                case 10:
                    if (_ds10_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds10_R);
                case 11:
                    if (_ds11_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds11_R);
                case 12:
                    if (_ds12_R == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds12_R);

            }

            return null;
        }


        /// <summary>
        /// 获取某天的Rate记录
        /// </summary>
        /// <param name="Date">日期</param>
        /// <returns></returns>
        public DataRow GetRow(DateTime Date)
        {

            DataRow[] drSearch = null;

            switch (Date.Month)
            {
                case 1:

                    if (_ds1_R.Tables.Count == 0) return null;

                    drSearch = _ds1_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 2:
                    if (_ds2_R.Tables.Count == 0) return null;
                    drSearch = _ds2_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 3:
                    if (_ds3_R.Tables.Count == 0) return null;
                    drSearch = _ds3_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 4:
                    if (_ds4_R.Tables.Count == 0) return null;
                    drSearch = _ds4_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 5:
                    if (_ds5_R.Tables.Count == 0) return null;
                    drSearch = _ds5_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 6:
                    if (_ds6_R.Tables.Count == 0) return null;
                    drSearch = _ds6_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 7:
                    if (_ds7_R.Tables.Count == 0) return null;
                    drSearch = _ds7_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 8:
                    if (_ds8_R.Tables.Count == 0) return null;
                    drSearch = _ds8_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 9:
                    if (_ds9_R.Tables.Count == 0) return null;
                    drSearch = _ds9_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 10:
                    if (_ds10_R.Tables.Count == 0) return null;
                    drSearch = _ds10_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 11:
                    if (_ds11_R.Tables.Count == 0) return null;
                    drSearch = _ds11_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 12:
                    if (_ds12_R.Tables.Count == 0) return null;
                    drSearch = _ds12_R.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

            }

            return null;
        }


    }
}
